package tv.youxing.android.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import tv.youxing.android.R;
import tv.youxing.android.adapter.EntertainmentAdapter;
import tv.youxing.android.adapter.FeaturedAdapter;
import tv.youxing.android.adapter.HomeFragment_Zhuanjifenlei_Adapter;
import tv.youxing.android.adapter.HotAdapter;
import tv.youxing.android.adapter.VideoAdapter;
import tv.youxing.android.entity.HomeModel;
import tv.youxing.android.entity.VideoPlayerPostModel;
import tv.youxing.android.entity.ZhuanJiFenLeiListModel;
import tv.youxing.android.ui.AdvertisementWEBActivity;
import tv.youxing.android.ui.HomeFragmentVideoMoreActivity;
import tv.youxing.android.ui.LiShiPlayerActivity;
import tv.youxing.android.ui.MoreClassificationActivity;
import tv.youxing.android.ui.SearchActivity;
import tv.youxing.android.ui.SubscribeDeteilsActivity;
import tv.youxing.android.ui.ZhuanJiFenLeiActivity;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.ListPageUtil;
import tv.youxing.android.utils.PicassoUtils;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;
import tv.youxing.android.utils.UtilTools;


/**
 * Created by apple on 2017/4/17.
 */

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .fragment
 * 文件名：   HomepageFragment
 * 创建者：   叶克武
 * 创建时间： 2017/4/17 下午2:34
 * 描述：    TODO
 */
public class HomepageFragment extends Fragment implements View.OnClickListener {

    private ImageView iv_search;
    private ImageView iv_history;

    //所有数据
    private HomeModel mAllDatas;

    @BindView(R.id.scrollview)
    ScrollView mScrollView;

    //顶部广告栏控件
    @BindView(R.id.convenientBanner)
    ConvenientBanner convenientBanner;
    private List<HomeModel.ResultBean.BannersBean> adModelList = new ArrayList<>();

    //专辑分类
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager mtabViewPager;
    private List<HomeModel.ResultBean.AlbumClassifysBean> tablayoutList = new ArrayList<>();
    ZhuanJiFenLeiListModel mZhuanJiFenLeiAlldate;
    List<ZhuanJiFenLeiListModel.ResultBean.DataBean> zjflList = new ArrayList<>();

    @BindView(R.id.zhuanjifenleilist)
    RecyclerView mZhuanJiFenLeiRecyclerView;
    HomeFragment_Zhuanjifenlei_Adapter mHomeFragment_zhuanjifenlei_adapter;


    //热门视频
    @BindView(R.id.videoGridView)
    GridView videoGridView;
    @BindView(R.id.More_popular_video)
    TextView tv_More_popular_video;
    @BindView(R.id.notvideochange)
    TextView tv_change_video;//换一换
    @BindView(R.id.module_1)
    TextView tv_video_module_name;
    private List<HomeModel.ResultBean.PopularVideoBean.ContListBean> videoList = new ArrayList<>();

    //热门推荐
    @BindView(R.id.featuredGridView)
    GridView hotGridView;
    @BindView(R.id.not_more)
    TextView hot_more;
    @BindView(R.id.module_2)
    TextView tv_hot_module_name;
    private List<HomeModel.ResultBean.PopularRecommenBean.ContListBeanXXX> hotList = new ArrayList<>();

    //精选推荐
    @BindView(R.id.hotGridView)
     GridView featuredGridView;
    @BindView(R.id.featured_more)
    TextView featured_more;
    @BindView(R.id.module_3)
    TextView tv_featured_module_name;
    private List<HomeModel.ResultBean.FeaturedRecommenBean.ContListBeanXX> featuredList = new ArrayList<>();


    //娱乐精选
    @BindView(R.id.enterGridView)
    GridView enterGridView;
    @BindView(R.id.yule_more)
    TextView enter_more;
    @BindView(R.id.module_4)
    TextView tv_enter_module_name;
    private List<HomeModel.ResultBean.EntertainmentRecommendBean.ContListBeanX> entertainmentList  = new ArrayList<>();


    //更多分类
    @BindView(R.id.more_fl)
    TextView more_fl;

    //广告
    @BindView(R.id.bottom_img)
    ImageView iv_Advertisement;
    HomeModel.ResultBean.AdBean advertisementData;


    //广播来设置是否更新
    public  static  boolean isUpdateData=false;

    //是否刷新更新
    public boolean isRefUpdate = false;


    private KProgressHUD hud;
    private VideoAdapter mVideoAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_homepage, null);
            ButterKnife.bind(this, view);
            findView(view);
        return view;
    }

    private void findView(View view) {
        EventBus.getDefault().register(this);

        showProgress("加载中");
        initData();
        //顶部搜索和历史按钮
        initView(view);

    }

    private void initData() {


        HttpParams params = new HttpParams();


        if (UtilTools.LoginJudge()) {
//            params.putHeaders("Authorization", "Bearer " + ShareUtils.getString(getContext(), AppConst.TOKEN, ""));
            params.put("user_id",ShareUtils.getInt(getContext(), AppConst.USERID, 0));
        }


        RxVolley.post(StaticClass.IndexURL, params, new HttpCallback() {

            @Override
            public void onSuccess(String t) {

                mAllDatas = GsonUtils.GsonToBean(t, HomeModel.class);

                if (mAllDatas.getCode() == 200) {
                    updateView(mAllDatas);
                }
            }
        });
    }

    /**
     * 取得服务器数据后更新界面
     */
    private void updateView(HomeModel hd) {



        if (hud.isShowing()) {
            hideProgress();
        }

        isUpdateData = false;


        //module_name
        tv_video_module_name.setText(hd.getResult().getPopular_video().getModule_name());
        tv_hot_module_name.setText(hd.getResult().getPopular_recommen().getModule_name());
        tv_featured_module_name.setText(hd.getResult().getFeatured_recommen().getModule_name());
        tv_enter_module_name.setText(hd.getResult().getEntertainment_recommend().getModule_name());

        //1 轮播图
        adModelList = hd.getResult().getBanners();
        initBannerView();


        //2.专辑分类
//        tablayoutList = hd.getResult().getAlbum_classifys();
       if (!isRefUpdate){
           tablayoutList.addAll(hd.getResult().getAlbum_classifys());
           initTabLayout();
       }

        //3.热门视频
        videoList = hd.getResult().getPopular_video().getCont_list();
        getData();

        //4.热门推荐
        hotList = hd.getResult().getPopular_recommen().getCont_list();
        initHotView();


        //5.精选推荐
        featuredList = hd.getResult().getFeatured_recommen().getCont_list();
        initFeaturedView();


        //6.娱乐精选
        entertainmentList = hd.getResult().getEntertainment_recommend().getCont_list();
         initEntertainment();

        //7.广告
        advertisementData = hd.getResult().getAd();
        initAdvertisement();

        //滑动到顶部
        mScrollView.smoothScrollTo(0,0);
    }


    //广告
    private void initAdvertisement() {
        PicassoUtils.loadImageView(getContext(), advertisementData.getCover(),iv_Advertisement);
    }

    //轮播图
    private void initBannerView() {

        //自定义你的Holder，实现更多复杂的界面，不一定是图片翻页，其他任何控件翻页亦可。
        convenientBanner.setPages(
                new CBViewHolderCreator<LocalImageHolderView>() {
                    @Override
                    public LocalImageHolderView createHolder() {
                        return new LocalImageHolderView();
                    }
                }, adModelList)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                //参数 1.未选中效果 2.选中效果
                .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
                //设置指示器的方向
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {


//                        Intent intent = new Intent(getActivity(), ZhuanJiFenLeiActivity.class);
//                        intent.putExtra("album_classify_id",adModelList.get(position).getBanner_id()+"");
//                        startActivity(intent);


                        Intent intent5 = new Intent(getActivity(), AdvertisementWEBActivity.class);
                        intent5.putExtra("url",mAllDatas.getResult().getBanners().get(position).getGo_to_url());
                        getContext().startActivity(intent5);





//                        AdModel adModel = adModelList.get(position);
//                        Intent intent = new Intent(getActivity(), BannerActivity.class);
//                        intent.putExtra("ad_url", adModel.getAd_url());
//                        startActivity(intent);
                    }
                });
        //设置翻页的效果，不需要翻页效果可用不设
        //.setPageTransformer(Transformer.DefaultTransformer);    集成特效之后会有白屏现象，新版已经分离，如果要集成特效的例子可以看Demo的点击响应。
//        convenientBanner.setManualPageable(false);//设置不能手动影响
    }


    //热门视频
    private void initVideoView(final List<HomeModel.ResultBean.PopularVideoBean.ContListBean> pagedList) {

        mVideoAdapter = new VideoAdapter(getContext(), pagedList);
        videoGridView.setAdapter(mVideoAdapter);
        videoGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent = new Intent(getActivity(), SubscribeDeteilsActivity.class);
//                intent.putExtra("multimedia_id",videoList.get(position).getMultimedia_id()+"");
//                getContext().startActivity(intent);


                HomeModel.ResultBean.PopularVideoBean.ContListBean m = pagedList.get(position);
                VideoPlayerPostModel model = new VideoPlayerPostModel();
                model.setMultimedia_id(m.getMultimedia_id());
                model.setTitle(m.getTitle());
                model.setIntro(m.getIntro());
                model.setCover(m.getCover());
                model.setDuration(m.getDuration());
                model.setPublish_at(m.getPublish_at());
                model.setPlay_url(m.getPlay_url());
                model.setType(m.getType());
                model.setPlay_count(m.getPlay_count());
                model.setComment_count(m.getComment_count());
                model.setLike_count(m.getLike_count());
                model.setCollection_count(m.getCollection_count());
                model.setStatus(m.getStatus());
                model.setCreated_at(m.getCreated_at());
                model.setUpdated_at(m.getUpdated_at());
                model.setAlbum_name(m.getAlbum_name());
                model.setIs_collectied(m.getIs_collectied());
                model.setIs_like(m.getIs_like());
                model.setVideo_share_url(m.getVideo_share_url());
//                List<VideoPlayerPostModel.RecommendMultimediasBean> videoRcList = new ArrayList<>();
//                for (int i = 0; i < m.getRecommend_multimedias().size(); i++) {
//                    VideoPlayerPostModel.RecommendMultimediasBean recommendMultimediasBean = new VideoPlayerPostModel.RecommendMultimediasBean();
//                    recommendMultimediasBean.setMultimedia_id(m.getRecommend_multimedias().get(i).getMultimedia_id());
//                    recommendMultimediasBean.setTitle(m.getRecommend_multimedias().get(i).getTitle());
//                    recommendMultimediasBean.setIntro(m.getRecommend_multimedias().get(i).getIntro());
//                    recommendMultimediasBean.setCover(m.getRecommend_multimedias().get(i).getCover());
//                    recommendMultimediasBean.setDuration(m.getRecommend_multimedias().get(i).getDuration());
//                    recommendMultimediasBean.setPublish_at(m.getRecommend_multimedias().get(i).getPublish_at());
//                    recommendMultimediasBean.setPlay_url(m.getRecommend_multimedias().get(i).getPlay_url());
//                    recommendMultimediasBean.setType(m.getRecommend_multimedias().get(i).getType());
//                    recommendMultimediasBean.setDownload_count(m.getRecommend_multimedias().get(i).getDownload_count());
//                    recommendMultimediasBean.setComment_count(m.getRecommend_multimedias().get(i).getComment_count());
//                    recommendMultimediasBean.setPlay_count(m.getRecommend_multimedias().get(i).getPlay_count());
//                    recommendMultimediasBean.setLike_count(m.getRecommend_multimedias().get(i).getLike_count());
//                    recommendMultimediasBean.setCollection_count(m.getRecommend_multimedias().get(i).getCollection_count());
//                    recommendMultimediasBean.setStatus(m.getRecommend_multimedias().get(i).getStatus());
//                    recommendMultimediasBean.setCreated_at(m.getRecommend_multimedias().get(i).getCreated_at());
//                    recommendMultimediasBean.setUpdated_at(m.getRecommend_multimedias().get(i).getUpdated_at());
//                    recommendMultimediasBean.setIs_like(m.getRecommend_multimedias().get(i).getIs_like());
//                    recommendMultimediasBean.setIs_collectied(m.getRecommend_multimedias().get(i).getIs_collectied());
//                    recommendMultimediasBean.setLike_count(m.getRecommend_multimedias().get(i).getLike_count());
//                    recommendMultimediasBean.setCollection_count(m.getRecommend_multimedias().get(i).getCollection_count());
//                    recommendMultimediasBean.setUpdated_at(m.getRecommend_multimedias().get(i).getUpdated_at());
//                    VideoPlayerPostModel.RecommendMultimediasBean.AlbumBeanX ab = new VideoPlayerPostModel.RecommendMultimediasBean.AlbumBeanX();
//
//                    ab.setAlbum_id(m.getRecommend_multimedias().get(i).getAlbum().getAlbum_id());
//                    ab.setName(m.getRecommend_multimedias().get(i).getAlbum().getName());
//                    ab.setCover(m.getRecommend_multimedias().get(i).getAlbum().getCover());
//                    ab.setIs_order(m.getRecommend_multimedias().get(i).getAlbum().getIs_order());
//                    recommendMultimediasBean.setAlbum(ab);
//
//                    videoRcList.add(recommendMultimediasBean);
//                }
//                model.setRecommend_multimedias(videoRcList);
                VideoPlayerPostModel.AlbumBean albumBean = new VideoPlayerPostModel.AlbumBean();
                albumBean.setAlbum_id(m.getAlbum().getAlbum_id());
                albumBean.setName(m.getAlbum().getName());
                albumBean.setCover(m.getAlbum().getCover());
                albumBean.setIs_order(m.getAlbum().getIs_order());
                model.setAlbum(albumBean);

                Intent intent = new Intent(getActivity(), SubscribeDeteilsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data",model);
                intent.putExtras(bundle);
                getActivity().startActivity(intent);

            }
        });

    }


    //专辑分类
    private void initTabLayout() {


        mHomeFragment_zhuanjifenlei_adapter = new HomeFragment_Zhuanjifenlei_Adapter(getContext(),tablayoutList);
        LinearLayoutManager ms = new LinearLayoutManager(getContext());
        ms.setOrientation(LinearLayoutManager.HORIZONTAL);// 设置 recyclerview 布局方式为横向布局
        mZhuanJiFenLeiRecyclerView.setLayoutManager(ms);
        mZhuanJiFenLeiRecyclerView.setAdapter(mHomeFragment_zhuanjifenlei_adapter);

//        MyViewPagerAdapter adapter = new MyViewPagerAdapter();
//
//        LayoutInflater mLayoutInflater = getActivity().getLayoutInflater();
//
//        if (tablayoutList == null || tablayoutList.size() == 0) {
//            return;
//        }
//
//        //tablayoutList.clear();
//
//        for (int i = 0; i < tabLayout.getTabCount(); i++) {
//
//            tabLayout.clearChildFocus(tabLayout.getChildAt(i));
//        }
//
//        for (int i = 0; i < tablayoutList.size(); i++) {
//            TabLayout.Tab tab = tabLayout.newTab();
//
//            int index= i;
//            View customview = mLayoutInflater.inflate(R.layout.layout_item_tab, null);
//
//
//            ImageView iv = (ImageView) customview.findViewById(R.id.imageView);
//            TextView text = (TextView) customview.findViewById(R.id.textView);
//            tab.setCustomView(customview);
//
//            PicassoUtils.loadImageView(getContext(),tablayoutList.get(i).getCover().toString(),iv);
//            text.setText(tablayoutList.get(i).getAlbum_classify_name());
//
//
//
//            tabLayout.addTab(tab);
//
//
//        }
//
//
//        mtabViewPager.setAdapter(adapter);
//
//        mtabViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mtabViewPager));
//
//        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//
//            /**
//             * 重复选择调用
//             * @param tab
//             */
//            @Override
//            public void onTabSelected(final TabLayout.Tab tab) {
////                CustomToast.INSTANCE.showToast(tablayoutList.get(tab.getPosition()).getAlbum_classify_name()+"---"+tab.getPosition());
//
//                Intent intent = new Intent(getActivity(), ModuleMoreDetailsActivity.class);
//                        intent.putExtra("album_classify_id",tablayoutList.get(tab.getPosition()).getAlbum_classify_id()+"");
//                        startActivity(intent);
////                HttpParams params = new HttpParams();
////
////
////                RxVolley.post(StaticClass.ZhuanJiFenLeiList, params, new HttpCallback() {
////                    @Override
////                    public void onSuccess(String t) {
////                        mZhuanJiFenLeiAlldate = GsonUtils.GsonToBean(t,ZhuanJiFenLeiListModel.class);
////                        zjflList = mZhuanJiFenLeiAlldate.getResult().getData();
////
////
////
////
////
////                    }
////                });
//
//
//            }
//
//            /**
//             * 当tab被选中
//             * @param tab
//             */
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//
//            }
//
//            /**
//             * 选择完成
//             * @param tab
//             */
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//            }
//        });


        isRefUpdate=true;
    }


    //热门推荐
    private void initHotView() {
        hotGridView.setAdapter(new HotAdapter(getActivity(), hotList));
        hotGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                Logger.d("id是:" + hotList.get(position).getMultimedia_id());

                HomeModel.ResultBean.PopularRecommenBean.ContListBeanXXX m = hotList.get(position);
                VideoPlayerPostModel model = new VideoPlayerPostModel();
                model.setMultimedia_id(m.getMultimedia_id());
                model.setTitle(m.getTitle());
                model.setIntro(m.getIntro());
                model.setCover(m.getCover());
                model.setDuration(m.getDuration());
                model.setPublish_at(m.getPublish_at());
                model.setPlay_url(m.getPlay_url());
                model.setType(m.getType());
                model.setPlay_count(m.getPlay_count());
                model.setComment_count(m.getComment_count());
                model.setLike_count(m.getLike_count());
                model.setCollection_count(m.getCollection_count());
                model.setStatus(m.getStatus());
                model.setCreated_at(m.getCreated_at());
                model.setUpdated_at(m.getUpdated_at());
                model.setAlbum_name(m.getAlbum_name());
                model.setIs_collectied(m.getIs_collectied());
                model.setIs_like(m.getIs_like());
                model.setVideo_share_url(m.getVideo_share_url());
                VideoPlayerPostModel.AlbumBean albumBean = new VideoPlayerPostModel.AlbumBean();
                albumBean.setAlbum_id(m.getAlbum().getAlbum_id());
                albumBean.setName(m.getAlbum().getName());
                albumBean.setCover(m.getAlbum().getCover());
                albumBean.setIs_order(m.getAlbum().getIs_order());
                model.setAlbum(albumBean);

                Intent intent = new Intent(getActivity(), SubscribeDeteilsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data",model);
                intent.putExtras(bundle);
                getActivity().startActivity(intent);
            }
        });

    }

    //精选推荐
    private void initFeaturedView() {
        featuredGridView.setAdapter(new FeaturedAdapter(getContext(), featuredList));
        featuredGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                HomeModel.ResultBean.FeaturedRecommenBean.ContListBeanXX m = featuredList.get(position);
                VideoPlayerPostModel model = new VideoPlayerPostModel();
                model.setMultimedia_id(m.getMultimedia_id());
                model.setTitle(m.getTitle());
                model.setIntro(m.getIntro());
                model.setCover(m.getCover());
                model.setDuration(m.getDuration());
                model.setPublish_at(m.getPublish_at());
                model.setPlay_url(m.getPlay_url());
                model.setType(m.getType());
                model.setPlay_count(m.getPlay_count());
                model.setComment_count(m.getComment_count());
                model.setLike_count(m.getLike_count());
                model.setCollection_count(m.getCollection_count());
                model.setStatus(m.getStatus());
                model.setCreated_at(m.getCreated_at());
                model.setUpdated_at(m.getUpdated_at());
                model.setAlbum_name(m.getAlbum_name());
                model.setIs_collectied(m.getIs_collectied());
model.setIs_like(m.getIs_like());
                model.setVideo_share_url(m.getVideo_share_url());
                VideoPlayerPostModel.AlbumBean albumBean = new VideoPlayerPostModel.AlbumBean();
                albumBean.setAlbum_id(m.getAlbum().getAlbum_id());
                albumBean.setName(m.getAlbum().getName());
                albumBean.setCover(m.getAlbum().getCover());
                albumBean.setIs_order(m.getAlbum().getIs_order());
                model.setAlbum(albumBean);

                Intent intent = new Intent(getActivity(), SubscribeDeteilsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data",model);
                intent.putExtras(bundle);
                getActivity().startActivity(intent);
            }
        });

    }


    /**
     * 娱乐精选
     */
    private void initEntertainment() {
        enterGridView.setAdapter(new EntertainmentAdapter(getContext(),entertainmentList));
        enterGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                HomeModel.ResultBean.EntertainmentRecommendBean.ContListBeanX m = entertainmentList.get(position);
                VideoPlayerPostModel model = new VideoPlayerPostModel();
                model.setMultimedia_id(m.getMultimedia_id());
                model.setTitle(m.getTitle());
                model.setIntro(m.getIntro());
                model.setCover(m.getCover());
                model.setDuration(m.getDuration());
                model.setPublish_at(m.getPublish_at());
                model.setPlay_url(m.getPlay_url());
                model.setType(m.getType());
                model.setPlay_count(m.getPlay_count());
                model.setComment_count(m.getComment_count());
                model.setLike_count(m.getLike_count());
                model.setCollection_count(m.getCollection_count());
                model.setStatus(m.getStatus());
                model.setCreated_at(m.getCreated_at());
                model.setUpdated_at(m.getUpdated_at());
                model.setAlbum_name(m.getAlbum_name());
                model.setIs_collectied(m.getIs_collectied());
            model.setIs_like(m.getIs_like());
                model.setVideo_share_url(m.getVideo_share_url());
                VideoPlayerPostModel.AlbumBean albumBean = new VideoPlayerPostModel.AlbumBean();
                albumBean.setAlbum_id(m.getAlbum().getAlbum_id());
                albumBean.setName(m.getAlbum().getName());
                albumBean.setCover(m.getAlbum().getCover());
                albumBean.setIs_order(m.getAlbum().getIs_order());
                model.setAlbum(albumBean);

                Intent intent = new Intent(getActivity(), SubscribeDeteilsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data",model);
                intent.putExtras(bundle);
                getActivity().startActivity(intent);
            }
        });
    }





    private class MyViewPagerAdapter extends PagerAdapter {

        @Override
        public Object instantiateItem(ViewGroup container, int pos) {
//            TextView tv = new TextView(getActivity().getApplicationContext());
//            tv.setText("@ViewPager:" + pos);
//            tv.setTextSize(30.0f);
//            tv.setGravity(Gravity.CENTER);
//            tv.setTextColor(Color.BLACK);
//
//            container.addView(tv);

            return null;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getCount() {
            return tablayoutList.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

    }




    /**
     * 翻页控件的holder
     */
    public class LocalImageHolderView implements Holder<HomeModel.ResultBean.BannersBean> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, HomeModel.ResultBean.BannersBean data) {
            PicassoUtils.loadImageView(context,adModelList.get(position).getCover(),imageView);
        }


    }


    private void initView(View view) {



        iv_search = (ImageView) view.findViewById(R.id.iv_search);
        iv_search.setOnClickListener(this);
        iv_history = (ImageView) view.findViewById(R.id.iv_history);
        iv_history.setOnClickListener(this);



        //注册广播
        MyReceiver receiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.MY_BROADCAST");
        getActivity().registerReceiver(receiver, filter);
    }



    public void showProgress(String message)
    {
        if(hud==null)
        {
            hud = KProgressHUD.create(getContext())
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setLabel(message)
                    .setCancellable(true);
        }
        if(!hud.isShowing())
        {
            hud.show();
        }
    }
    public void hideProgress()
    {
        if(hud != null && hud.isShowing())
        {
            hud.dismiss();
        }
    }


    @Override
    public void onResume() {
        super.onResume();


        //开始自动翻页
        convenientBanner.startTurning(5000);

        if (isUpdateData) {
            initData();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        //停止翻页
        convenientBanner.stopTurning();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_history://历史播放记录
                startActivity(new Intent(getActivity(), LiShiPlayerActivity.class));
                break;

            case R.id.iv_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;

        }
    }
     HomeModel mHomeModel;


    int refCount=0;
    private List<HomeModel.ResultBean.PopularVideoBean.ContListBean> videoPopularList = new ArrayList<>();



    void getData() {
        refCount++;
        List<List<HomeModel.ResultBean.PopularVideoBean.ContListBean>> list = Arrays.asList(videoList);
        //数据源,请求的页数,每页返回的条数
        ListPageUtil<HomeModel.ResultBean.PopularVideoBean.ContListBean> listPageUtil = new ListPageUtil<HomeModel.ResultBean.PopularVideoBean.ContListBean>(list.get(0),refCount, 6);
        List<HomeModel.ResultBean.PopularVideoBean.ContListBean> pagedList = listPageUtil.getPagedList();
        if (pagedList.size()<1) {
            refCount=0;
            getData();
            return;
        }


        initVideoView(pagedList);

//
//
//        for (int i = 0; i < pagedList.size(); i++) {
//            System.out.println(pagedList.get(i).getTitle());
//
//
//
//        }
    }

    @OnClick({R.id.More_popular_video,R.id.notvideochange,R.id.featured_more,R.id.not_more,R.id.yule_more,R.id.more_fl,R.id.bottom_img})
    public void click(View view) {

        switch (view.getId()) {
            case R.id.More_popular_video://热门视频_更多视频

//                ZhuanJiFenLeiActivity zhuanJiFenLeiActivity = new ZhuanJiFenLeiActivity();
//
//                Intent intent = new Intent(getActivity(), zhuanJiFenLeiActivity.getClass());
//                intent.putExtra("album_classify_id",mAllDatas.getResult().getPopular_video().getModule_id()+"");
//
//                L.d("album_classify_id:" + mAllDatas.getResult().getPopular_video().getModule_id());
//
//                startActivity(intent);


                Intent intent = new Intent(getContext(),HomeFragmentVideoMoreActivity.class);
                intent.putExtra("data", (Serializable) videoList);
                startActivity(intent);



                break;
            case R.id.notvideochange://换一换_更多视频
                 getData();


                break;
            case R.id.featured_more://热门推荐_更多
                Intent intent2 = new Intent(getActivity(), ZhuanJiFenLeiActivity.class);
                intent2.putExtra("album_classify_id",mAllDatas.getResult().getPopular_recommen().getModule_id()+"");
                startActivity(intent2);
                break;
            case R.id.not_more://精选推荐
                Intent intent3 = new Intent(getActivity(), ZhuanJiFenLeiActivity.class);
                intent3.putExtra("album_classify_id",mAllDatas.getResult().getFeatured_recommen().getModule_id()+"");
                startActivity(intent3);
                break;
            case R.id.yule_more://娱乐精选
                Intent intent4 = new Intent(getActivity(), ZhuanJiFenLeiActivity.class);
                intent4.putExtra("album_classify_id",mAllDatas.getResult().getEntertainment_recommend().getModule_id()+"");
                startActivity(intent4);
                break;
            case R.id.more_fl://查看更多分类
                startActivity(new Intent(getActivity(), MoreClassificationActivity.class));
                break;
            case R.id.bottom_img://广告

                //跳转到外部浏览器
//                Uri uri = Uri.parse("http://"+mAllDatas.getResult().getAd().getGo_to_url());
//                Intent intent5 = new Intent(Intent.ACTION_VIEW, uri);
//                startActivity(intent5);

                Intent intent5 = new Intent(getActivity(), AdvertisementWEBActivity.class);
                intent5.putExtra("url",mAllDatas.getResult().getAd().getGo_to_url());
                getContext().startActivity(intent5);
                break;
        }
    }


    class MyReceiver extends BroadcastReceiver  {

        @Override
        public void onReceive(Context context, Intent intent) {

            String operation = intent.getStringExtra("operation");

            if (operation.equals("update")) {
                isUpdateData=true;
            }
        }
    }




    @Subscribe
    public void onEventMainThread(String event) {
        if (event.equals("home")) {
            initData();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
